package com.qhgrain.api.menurolepermission.provider;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.ws.rs.core.Response;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.paas.base.modules.utils.lang.StringUtil;
import com.paas.base.modules.utils.page.PageBean;
import com.pubframework.common.vo.RestVo;
import com.qhgrain.api.menurolepermission.entity.Permission;
import com.qhgrain.api.menurolepermission.entity.RolePermission;
import com.qhgrain.api.menurolepermission.info.PermissionInfo;
import com.qhgrain.api.menurolepermission.service.IPermissionService;
import com.qhgrain.api.menurolepermission.service.IRolePermissionService;
import com.qhgrain.gway.util.UUIDUtil;
@Service
public class PermissionServiceProvider implements com.qhgrain.api.menurolepermission.IPermissionService{

	@Autowired
	private IPermissionService permissionServiceImpl;
	
	@Autowired
	private IRolePermissionService rolePermissionServiceImpl;

	@Override
	public Response add(String name, String type, String menuId, String pvalue,
			String description) {
		RestVo vo = new RestVo();
		try {
			//验证pvalue,要保证唯一性
			Permission pp = this.permissionServiceImpl.queryByPvalue(pvalue);
			if(pp != null){
				vo.setFail("保存失败，权限值是唯一性，不能重复");
			}else{
				String id = UUIDUtil.getUUID();
				Permission p = new Permission(id,menuId,name,type,pvalue,description);
				this.permissionServiceImpl.add(p);
				vo.setSuccess(id);
			}
			
		} catch (Exception e) {
			vo.setFail(e.getMessage());
		}
		return Response.ok(vo).build();
	}

	@Override
	public Response edit(String id, String name, String type, String menuId,
			String pvalue,String description) {
		RestVo vo = new RestVo();
		try {
			Permission p = this.permissionServiceImpl.find(id);
			if(p == null){
				vo.setFail("根据id找不到对应的对象");
			}else{
				//验证pvalue,要保证唯一性
				Permission pp = this.permissionServiceImpl.queryByPvalue(pvalue);
				if(pp != null && !pp.getId().equals(p.getId())){
					vo.setFail("pvalue不能重复");
				}else{
					p.setName(name);
					p.setType(type);
					p.setMenuId(menuId);
					p.setPvalue(pvalue);
					p.setDescription(description);
					this.permissionServiceImpl.update(p);
					vo.setSuccess("");
				}
			}
			
		} catch (Exception e) {
			vo.setFail(e.getMessage());
		}
		return Response.ok(vo).build();
	}

	@Override
	public Response queryPage(String id, String name, String type,
			String menuName, String appId, String pageNo, String pageSize) {
		RestVo vo = new RestVo();
		try {
			PageBean<PermissionInfo> page = this.permissionServiceImpl.queryPage(id, name, type, menuName, appId, Integer.parseInt(pageNo), Integer.parseInt(pageSize));
			JSONObject json = new JSONObject();
			json.put("list", page.getData());
			json.put("totalPage", page.getTotalPage());
			json.put("totalCount", page.getTotalCount());
			
			vo.setSuccess(json);
		} catch (Exception e) {
			vo.setFail(e.getMessage());
		}
		return Response.ok(vo).build();
	}

	@Override
	public Response getPermission(String permissionId) {
		RestVo vo = new RestVo();
		try {
			PermissionInfo p = this.permissionServiceImpl.findInfo(permissionId);
			if(p == null){
				vo.setFail("根据该id找不到对应的对象");
				vo.setData(new JSONObject());
			}else{
				vo.setSuccess(p);
			}
			
		} catch (Exception e) {
			vo.setFail(e.getMessage());
		}
		return Response.ok(vo).build();
	}

	@Override
	public Response delete(String ids) {
		RestVo vo = new RestVo();
		try {
			List<String> list = Arrays.asList(ids.split(","));
			this.permissionServiceImpl.delete(list);
			vo.setSuccess("");
		} catch (Exception e) {
			e.printStackTrace();
			vo.setFail(e.getMessage());
		}
		return Response.ok(vo).build();
	}

	@Override
	public Response queryByPvalue(String pvalue) {
		RestVo vo = new RestVo();
		try {
			if(StringUtil.isBlank(pvalue)){
				vo.setFail("pvalue不能为空");
			}else{
				Permission p = this.permissionServiceImpl.queryByPvalue(pvalue);
				vo.setSuccess(p);
			}
			
		} catch (Exception e) {
			vo.setFail(e.getMessage());
		}
		return Response.ok(vo).build();
	}

	@Override
	public Response queryRoleIds(String permissionId) {
		RestVo vo=new RestVo();
		try {
			List<RolePermission> rps=this.rolePermissionServiceImpl.queryByPermissionId(permissionId);
			List<String> list = new ArrayList<String>();
			for(RolePermission p: rps){
				list.add(p.getPermissionId());
			}
			vo.setSuccess(list);
		} catch (Exception e) {
			vo.setFail(e.getMessage());
		}
		
		return Response.ok(vo).build();
	}

	@Override
	public Response queryBymenus(String menuIds) {
		RestVo vo = new RestVo();
		try {
			List<String> list = Arrays.asList(menuIds.split(","));
			vo.setSuccess(this.permissionServiceImpl.queryByMenus(list));
		} catch (Exception e) {
			vo.setFail(e.getMessage());
		}
		return Response.ok(vo).build();
	}

	@Override
	public Response queryByUserApp(String userId, String appId,String permissionType) {
		RestVo vo = new RestVo();
		try {
			vo.setSuccess(this.permissionServiceImpl.queryByUserApp(userId, appId, permissionType));
		} catch (Exception e) {
			vo.setFail(e.getMessage());
		}
		return Response.ok(vo).build();
	}
	
	public Response queryByRoleApp(String roleId, String appId,String permissionType) {
		RestVo vo = new RestVo();
		try {
			vo.setSuccess(this.permissionServiceImpl.queryByRoleApp(roleId, appId, permissionType));
		} catch (Exception e) {
			vo.setFail(e.getMessage());
		}
		return Response.ok(vo).build();
	}
	
	
	public Response getPermission(String roleId,String appId,String permissionType) {
		RestVo vo=new RestVo();
		try {
			vo.setSuccess(this.permissionServiceImpl.queryByUserApp(roleId, appId, permissionType));
		} catch (Exception e) {
			vo.setFail(e.getMessage());
		}
		
		return Response.ok(vo).build();
	}
}
